package com.lfm.jdbc;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class exerciseM5 {

    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
        //1. 注册驱动-自动化

        //2. 获取连接
        Properties properties = new Properties();
        properties.load(new FileInputStream("mariadb.properties"));

        String url = properties.getProperty("url");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String user = properties.getProperty("user");
        Class.forName(driver);

        Connection connection = DriverManager.getConnection(url, user, password);

        //3. 执行sql
        String sql = "create table news(id int, newsreport varchar(32));";

        Statement statement = connection.createStatement();
        int rows = statement.executeUpdate(sql);

        sql = "insert into news values(1, '北京新闻');";
        rows = statement.executeUpdate(sql);
        sql = "insert into news values(2, '成都新闻');";
        rows = statement.executeUpdate(sql);
        sql = "insert into news values(3, '重庆新闻');";
        rows = statement.executeUpdate(sql);
        sql = "insert into news values(4, '南京新闻');";
        rows = statement.executeUpdate(sql);
        sql = "insert into news values(5, '上海新闻');";
        rows = statement.executeUpdate(sql);

        sql = "update news set newsreport = '南丹新闻' where id = 1;";
        rows = statement.executeUpdate(sql);
        sql = "delete from news where id = 3;";
        rows = statement.executeUpdate(sql);

        System.out.println(rows > 0 ? "成功" : "失败");
        //4. 关闭连接资源
        statement.close();
        connection.close();
    }
}
